/*
 * search_thread.h
 *
 *  Created on: 23.06.2013
 *      Author: SpirTanol
 */

#ifndef SEARCH_THREAD_H_
#define SEARCH_THREAD_H_

// QT
#include <QThread>

class DataPacket;
class SearchPacket;
class DeviceInfo;
class SearchEvent;
class ModbusPort;

class SearchThread : public QThread
{
	Q_OBJECT

	int oPort;
	int oAddr;
	int oFormat;
	int oSpeed;

	int oOneAddr;
	int oOneFormat;
	bool oOneTest;

	int oStep;

	ModbusPort *oMPort;
	bool oDestroyed;

public:
	SearchThread(QObject *parent = 0);
	~SearchThread();

	inline void setPort(int port) { oPort = port; }
	inline int port() const { return oPort; }

	inline void setAddress(int addr) { oAddr = addr; }
	inline int address() const { return oAddr; }

	inline void setSpeed(int speed) { oSpeed = speed; }
	inline int speed() const { return oSpeed; }

	inline void setFormat(int format) { oFormat = format; }
	inline int format() const { return oFormat; }

	void oneTest(int addr, int format);
	inline bool isOneTest() const { return oOneTest; }

	inline int step() const { return oStep; }

	void reset();

	void startSearch();
	void stopSearch();
	void toggleSearch();

	/** true если поиск завершён */
	inline bool isComplete() const { return (oStep == 0 || oStep == (247* 3)); }

private:
	void run(); // [virtual]

	bool readPacket(DataPacket *packet);

	static DeviceInfo *createDevice(const SearchPacket &packet);

private slots:
	void stoped();
	void searchFinished();

signals:
	void searchEvent(SearchEvent *e);

};

#endif /* SEARCH_THREAD_H_ */
